package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomizeException;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    /**
     * 满足条件时删除分类
     * @param id
     */
    @Override
    public void deleteCategoryByCondition(Long id) {
        // 查询分类关联的菜品
        Integer countDish = dishService.lambdaQuery().eq(Dish::getCategoryId, id).count();
        if (countDish > 0) {
            throw new CustomizeException("分类关联【菜品】，请先删除分类关联的菜品");
        }
        // 查询分类关联的套餐
        Integer countSetMeal = setmealService.lambdaQuery().eq(Setmeal::getCategoryId, id).count();
        if (countSetMeal > 0) {
            throw new CustomizeException("分类关联【套餐】，请先删除分类关联的套餐");
        }
        // 根据分类id删除分类
//        this.removeById(id);
        categoryMapper.deleteById(id);
    }
}
